// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    loading_view:fgui.GComponent;
    loading_per:number = 0;

    tips_time_count:number = 0;     //加载提示  计时器
    current_loading_tips:number = 0;    //加载提示  当前是哪个提示
    loading_tips = ["第一次进入游戏加载时间会比较久...","小猫睡觉状态不能旅行哦","小猫可通过放生来刷新它的技能"];

    onLoad () { 
        var reg = new RegExp("(^|&)" + 'token' + "=([^&]*)(&|$)" + "targetUserId" + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        var url = window.location.href;
        let url_temp = url.split('?');
        //console.log('url_temp',url_temp)
        let url_temp1 = url_temp[1].split('&');
        //console.log('url_temp1',url_temp1)
        let token = url_temp1[1].split('=')[1];
        orc.orc_data.memory_data.go_friend = '';
        if(url_temp1[2])
        {            
           orc.orc_data.memory_data.go_friend = url_temp1[2].split('=')[1];
        }
        
        fgui.UIPackage.loadPackage("fairygui/loading/game_loading", this.game_loading.bind(this)); 


    }

    game_loading(){
        fgui.GRoot.create();
        this.loading_view = fgui.UIPackage.createObject("loading","loading").asCom;//包名,组件名
        this.loading_view.makeFullScreen();
        fgui.GRoot.inst.addChild(this.loading_view); 
       
        let res = [
            //大厅
            "fairygui/hall/game_hall_atlas0",
            "fairygui/hall/game_hall",
            //弹窗 
            "fairygui/window/game_window_atlas0",
            "fairygui/window/game_window",
        ];

        var asd = (completedCount,totalCount) => {
            console.log("completedCount:" + completedCount + ",totalCount:" + totalCount );
            this.loading_per = (completedCount/totalCount * 100) > this.loading_per ? (completedCount/totalCount * 100):this.loading_per;
            this.loading_view.getChild('n0').asProgress.value = this.loading_per;  
        };
        
        cc.resources.load(res,asd,(err,assets)=>{ 
            fgui.UIPackage.addPackage("fairygui/hall/game_hall");  
            fgui.UIPackage.addPackage("fairygui/window/game_window");
            cc.director.loadScene("game_hall");        
            fgui.UIPackage.removePackage("loading");            
        })
    }

    update (dt) {
        this.tips_time_count += dt;
        if(this.tips_time_count > 3)
        {
            this.tips_time_count = 0;
            this.current_loading_tips ++;
            if(this.current_loading_tips >= this.loading_tips.length)
            {
                this.current_loading_tips = 0;
            }
            
            this.loading_view.getChild('n1').asTextField.text = this.loading_tips[this.current_loading_tips];
            //this.$loading_tips.string = this.loading_tips[this.current_loading_tips];
        }     
    }
}
